counties <- read_sf("./data/NC_counties-shp")
glimpse(counties)
## Observations: 100
## Variables: 11
## $ FID        <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17…
## $ OBJECTID   <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17…
## $ PERIMETER  <dbl> 156473.0, 166183.5, 143114.5, 116938.2, 180894.2, 174751.…
## $ CO_NAME    <chr> "ROCKINGHAM", "GUILFORD", "ALAMANCE", "ALEXANDER", "RANDO…
## $ CO_ABBR    <chr> "ROCK", "GUIL", "ALAM", "ALEX", "RAND", "WAYN", "ASHE", "…
## $ ACRES      <dbl> 366053.1, 420968.9, 278127.0, 168641.0, 505668.0, 356469.…
## $ Shape_Leng <dbl> 513361.8, 545220.4, 469535.0, 383654.7, 593483.6, 573331.…
## $ RO         <chr> "WSRO", "WSRO", "WSRO", "MRO", "WSRO", "WARO", "WSRO", "W…
## $ SHAPE_Le_1 <dbl> 194502.4, 205797.6, 177273.0, 144599.5, 223033.8, 214578.…
## $ SHAPE_Area <dbl> 15945261643, 18337394619, 12115206198, 7345996919, 220268…
## $ geometry   <POLYGON [m]> POLYGON ((-8853514 4333890,..., POLYGON ((-890947…
library(RColorBrewer)

ggplot(data = counties, aes(fill = ACRES)) +
  geom_sf() +
  theme(
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    rect = element_blank()
  ) +
  labs(
    title = "North Carolina Counties",
    fill = "Acres"
  ) +
  scale_fill_distiller(palette = "Purples")

# leaflet
library(leaflet)

leaflet(data = counties) %>%
  addTiles() %>%
  addPolygons()
## Warning: sf layer is not long-lat data
## Warning: sf layer has inconsistent datum (+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs).
## Need '+proj=longlat +datum=WGS84'
st_crs(counties)
## Coordinate Reference System:
##   User input: 3857 
##   wkt:
## PROJCS["WGS 84 / Pseudo-Mercator",
##     GEOGCS["WGS 84",
##         DATUM["WGS_1984",
##             SPHEROID["WGS 84",6378137,298.257223563,
##                 AUTHORITY["EPSG","7030"]],
##             AUTHORITY["EPSG","6326"]],
##         PRIMEM["Greenwich",0,
##             AUTHORITY["EPSG","8901"]],
##         UNIT["degree",0.0174532925199433,
##             AUTHORITY["EPSG","9122"]],
##         AUTHORITY["EPSG","4326"]],
##     PROJECTION["Mercator_1SP"],
##     PARAMETER["central_meridian",0],
##     PARAMETER["scale_factor",1],
##     PARAMETER["false_easting",0],
##     PARAMETER["false_northing",0],
##     UNIT["metre",1,
##         AUTHORITY["EPSG","9001"]],
##     AXIS["X",EAST],
##     AXIS["Y",NORTH],
##     EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"],
##     AUTHORITY["EPSG","3857"]]
counties_4326 <- counties %>%
  st_transform(4326)
st_crs(counties_4326)
## Coordinate Reference System:
##   User input: EPSG:4326 
##   wkt:
## GEOGCS["WGS 84",
##     DATUM["WGS_1984",
##         SPHEROID["WGS 84",6378137,298.257223563,
##             AUTHORITY["EPSG","7030"]],
##         AUTHORITY["EPSG","6326"]],
##     PRIMEM["Greenwich",0,
##         AUTHORITY["EPSG","8901"]],
##     UNIT["degree",0.0174532925199433,
##         AUTHORITY["EPSG","9122"]],
##     AUTHORITY["EPSG","4326"]]
# leaflet
library(leaflet)

leaflet(data = counties_4326) %>%
  addTiles() %>%
  addPolygons()
ncssm <- tribble(
  ~lat, ~lon, ~name,
  36.02, -78.92, "Durham",
  35.73, -81.69, "Morganton"
)
leaflet() %>%
  addTiles() %>%
  addPolygons(data = counties_4326,
              label = ~CO_NAME) %>%
  addMarkers(data = ncssm,
             popup = ~name)
## Assuming "lon" and "lat" are longitude and latitude, respectively
library(RColorBrewer)

pal <- colorNumeric(
  palette = "Purples",
  domain = counties_4326$ACRES
)
leaflet() %>%
  addTiles() %>%
  addPolygons(data = counties_4326,
              label = ~CO_NAME,
              color = ~pal(ACRES),
              fillOpacity = 0.8) %>%
  addMarkers(data = ncssm,
             popup = ~name)
## Assuming "lon" and "lat" are longitude and latitude, respectively